package com.getpebble.android.core.auth.account;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;
import com.getpebble.android.R;
import com.getpebble.android.common.auth.PblSessionManager;
import com.getpebble.android.core.auth.activity.LoginActivity;

public class PblAccountAuthenticator extends AbstractAccountAuthenticator {
    private static Handler sHandler = new Handler(Looper.getMainLooper());
    private Context mContext;

    public PblAccountAuthenticator(Context context) {
        super(context);
        this.mContext = context;
    }

    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        throw new UnsupportedOperationException();
    }

    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
        Bundle result = new Bundle();
        if (isAccessTokenCached(this.mContext, PblSessionManager.getPebbleAccount())) {
            sHandler.post(new Runnable() {
                public void run() {
                    Toast.makeText(PblAccountAuthenticator.this.mContext, PblAccountAuthenticator.this.mContext.getString(R.string.login_only_one_account), 0).show();
                }
            });
            result.putInt("errorCode", 9);
            result.putString("errorMessage", this.mContext.getString(R.string.login_only_one_account));
        }
        Intent intent = new Intent(this.mContext, LoginActivity.class);
        intent.putExtra("accountAuthenticatorResponse", response);
        result.putParcelable("intent", intent);
        return result;
    }

    private boolean isAccessTokenCached(Context context, Account account) {
        AccountManager accountManager = AccountManager.get(context);
        if (accountManager == null || account == null || accountManager.peekAuthToken(account, "com.getpebble") == null) {
            return false;
        }
        return true;
    }

    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }

    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
        Bundle result;
        if (authTokenType.equals("com.getpebble")) {
            result = new Bundle();
            AccountManager accountManager = AccountManager.get(this.mContext);
            if (accountManager != null) {
                String authToken = accountManager.peekAuthToken(account, authTokenType);
                if (!TextUtils.isEmpty(authToken)) {
                    result.putString("authAccount", account.name);
                    result.putString("accountType", account.type);
                    result.putString("authtoken", authToken);
                    return result;
                }
            }
            result.putParcelable("intent", new Intent(this.mContext, LoginActivity.class));
            return result;
        }
        result = new Bundle();
        result.putString("errorMessage", this.mContext.getString(R.string.login_invalid_token_type));
        return result;
    }

    public String getAuthTokenLabel(String authTokenType) {
        if (authTokenType.equals("com.getpebble")) {
            return this.mContext.getString(R.string.app_name);
        }
        return null;
    }

    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }

    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
}
